<?php
/**
 * FlickrPhotosetsLogger class file
 * @package FlickrPhotosetsLogging
 */

/**
 * This class handles the logging of various operations.
 * 
 * @package Facebook
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 7.10.17
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 * @since Class available since Release 2.0
 */
class FlickrPhotosetsLogger {
	
	/**
	 * Database object reference
	 * @var FlickrPhotosetsDatabase
	 */
	private $db;
	
	/**
	 * Constructor
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param FlickrPhotosetsDatabase $db The FlickrPhotosetsDatabase object
	 */
	public function __construct($db) {
		$this->db = $db;
	}
	
	/**
	 * Logs an install of the application.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $uid The uid of the adding user
	 * @return boolean True if the query was successful
	 */
	public function install($uid) {
		return $this->db->log_install($uid);
	}
	
	/**
	 * Logs a news feed notification.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $uid The uid of the user being notified
	 * @param string $type The type of notification, either photos or photosets
	 * @param boolean $success Whether the notification was a success
	 * @param string $extra Extra information about the notification
	 * @return boolean True if the query was successful
	 */
	public function notification($uid, $type, $success, $extra = '') {
		return $this->db->log_notification($uid, $type, $success, $extra);
	}
	
	/**
	 * Logs a removal of the application.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $uid The uid of the removing user
	 * @param boolean $success Whether the notification was a success
	 * @param string $extra Extra information about the notification
	 * @return boolean True if the query was successful
	 */
	public function remove($uid, $success = true, $extra = '') {
		return $this->db->log_remove($uid, $success, $extra);
	}
	
	/**
	 * Logs a profile update.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $uid The uid of the updating user
	 * @return boolean True if the query was successful
	 */
	public function update($uid, $extra = '') {
		return $this->db->log_update($uid, $extra);
	}
	
	
	
	/*******************************************************
	
	These methods are not completed yet!
	
	*******************************************************/
	
	
	/**
	 * Gets the number of installs since a specific time.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $since The time to get installs from
	 * @return int The number of installs
	 */
	static public function get_installs($since) {
		$query = sprintf(
			"SELECT COUNT(*) FROM %s_logs WHERE time > '%s' AND action='add'",
			DB_TABLE,
			ApplicationLogger::sql_clean($since)
		);
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result)) {
			$installs = $row['COUNT(*)'];
		}
		return $installs;
	}
	
	/**
	 * Gets the last stats update.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return The row if the query was successful
	 */
	static public function get_last_stats() {
		$query = sprintf(
			"SELECT time, users FROM %s_stats ORDER BY time DESC LIMIT 1",
			DB_TABLE
		);
		$result = mysql_query($query);
		if ($row = mysql_fetch_assoc($result)) {
			return $row;
		}
		return false;
	}
	
	/**
	 * Gets the number of removes since a specific time.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $since The time to get removes from
	 * @return int The number of removes
	 */
	public function get_removes($since) {
		$query = $this->db->mysql_select_query('log', 'COUNT(*)', array('action' => 'rem'), 1, false);
		$result = mysql_query($query, $this->db->db);
		while ($row = mysql_fetch_array($result)) {
			$removes = $row['COUNT(*)'];
		}
		return $removes;
	}
	
	/**
	 * Records the stats for a given time.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param int $installs The number of installs
	 * @param int $removes The number of removes
	 * @param int $the number of users
	 * @return boolean True if the query was successful
	 */
	static public function record_stats($installs, $removes, $users) {
		$query = sprintf(
			"INSERT INTO %s_stats (time, installs, removes, users) VALUES (now(), '%d', '%d', '%d')",
			DB_TABLE,
			$installs,
			$removes,
			$users
		);
		mysql_query($query) or die();
		return true;
	}
	
}

?>